package com.sanyou.controller.app;

import com.sanyou.pojo.Product;
import com.sanyou.pojo.SearchHistory;
import com.sanyou.pojo.dto.ProductInfoDTO;
import com.sanyou.pojo.vo.OrderVo;
import com.sanyou.pojo.vo.ProductVo;
import com.sanyou.pojo.vo.ProjectVo;
import com.sanyou.service.OrderService;
import com.sanyou.service.ProductService;
import com.sanyou.service.ProjectService;
import com.sanyou.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * User: asus
 * Date: 2021-12-16
 * Time: 15:33
 * Version:V1.0
 */
@Api(value = "App相关的接口",tags = {"App相关的Controller"})
@CrossOrigin
@RestController
@RequestMapping("/app")
public class AppController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ProductService productService;

    @GetMapping("/project/getProjectList")
    public JSONResult getProjectList(String userId){
        if(StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("用户Id为空!");
        }

        List<ProjectVo> projectList = projectService.getProjectList(userId);

        return JSONResult.ok(projectList);
    }

    @GetMapping("/order/getOrderList")
    public JSONResult getOrderList(Integer projectId){
        if(projectId == null)
            return JSONResult.errorMsg("项目不存在!");

        ProjectVo projectVo = projectService.getProject(projectId);
        if(projectVo == null)
            return JSONResult.errorMsg("项目不存在!");

        List<OrderVo> orderList = orderService.getProjectOrder(projectId);
        projectVo.setOrderList(orderList);

        return JSONResult.ok(projectVo);
    }


    @ApiImplicitParams({@ApiImplicitParam(name="code",value = "阴极板编码",required = true,
            dataType = "string", paramType = "query")})
    @GetMapping("/product/checkExist")
    public JSONResult checkExist(String code){
        if(StringUtils.isBlank(code))
            return JSONResult.errorMsg("阴极板不存在");

        Product product = productService.getProductByCode(code);
        if(product != null)
            return JSONResult.ok();
        else
            return JSONResult.errorMsg("阴极板不存在");
    }

    @GetMapping("/scancode/getInfo")
    public JSONResult getInfo(String scanCode,String userId,String tag){
        /**
         * 参数合法性校验
         */
        if(StringUtils.isBlank(scanCode))
            return JSONResult.errorMsg("阴极板编码为空!");

        if(StringUtils.isBlank(userId))
            return JSONResult.errorMsg("用户Id不能为空!");

        /**
         * 是否添加搜索记录
         */
        boolean flag = false;
        if(StringUtils.isBlank(tag))
            flag = false;
        if("1".equals(tag))
            flag = true;

        /**
         * 根据阴极板编码获取对应阴极板信息
         */
        //ProductVo dataVo = productService.getInfo(scanCode,userId,flag);

        /**
         * 根据阴极板编码获取对应阴极板信息
         */
        ProductVo info= productService.getProductInfoByCode(scanCode); //获取基本信息

        //数据转换
        if(info != null){
//            vo.setId(info.getId()); //阴极板id
//            vo.setProductCode(info.getProductCode()); //阴极板编码
//            vo.setProductPlane(info.getProductPlane()); //平面度
//            vo.setProductValue(info.getProductValue()); //垂直度
//            vo.setProductState(info.getProductState()); //阴极板状态
//            vo.setCreatetime(info.getCreatetime()); //创建时间
//            vo.setUpdateStateDate(info.getUpdateStateDate()); //状态更新日期
//            vo.setMaintainType(info.getMaintainType()); //维修类型
//            vo.setUpdateMaintainDate(info.getUpdateMaintainDate());  //维修日期
//            vo.setCycle(info.getCycle()); //循环次数
//            vo.setMaintainCycle(info.getMaintainCycle()); //维修次数

            info.setCollectStatus(productService.productIsCollected(scanCode,userId)?1:2); //是否被用户收藏
            info.setDdbSize(info.DdbSizeToString()); //导电棒尺寸格式化显示
            info.setYjbSize(info.YjbSizeToString()); //阴极板尺寸格式化显示

            /**
             * 添加搜索记录
             */
            if("1".equals(tag)){
                productService.insertSearchHistory(scanCode,userId);
            }
        }

        return JSONResult.ok(info);
    }

    @PostMapping("/product/updateState")
    public JSONResult updateState(@RequestBody ProductVo productVo){
        if(StringUtils.isBlank(productVo.getProductCode()))
            return JSONResult.errorMsg("阴极板不存在!");

        if(StringUtils.isBlank(productVo.getUserid()))
            return JSONResult.errorMsg("用户id不能为空!");

        try {
            productService.updateState(productVo);
        }catch (Exception ex){
            JSONResult.errorMsg(ex.getMessage());
        }
        return JSONResult.ok();
    }
}
